package com.hgyc.mom.wms.controller;

import java.util.*;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import com.hgyc.mom.wms.entity.WmsProductPick;
import com.hgyc.mom.wms.vo.WmsProductPickParam;
import com.hgyc.mom.wms.service.WmsProductPickService;
import com.hgyc.mom.wms.vo.WmsProductPickVO;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 生产领料单Controller
 *
 * @author fwj
 * @since 2025-07-09
 */
@Tag(name = "生产领料单控制器", description = "生产领料单控制器")
@RestController
@RequestMapping("/wms/product/pick")
public class WmsProductPickController {
    @Autowired
    private WmsProductPickService wmsProductPickService;

    @Operation(summary = "查询生产领料单列表", description = "查询生产领料单列表")
    @PostMapping("/pageList")
    public Result<IPage<WmsProductPick>> pageList(@Parameter(description = "查询参数") @RequestBody WmsProductPickParam wmsProductPickParam) {
        Page<WmsProductPick> page = new Page<>(wmsProductPickParam.getPage(), wmsProductPickParam.getPageSize());
        WmsProductPick wmsProductPick = new WmsProductPick();
        BeanUtils.copyProperties(wmsProductPickParam, wmsProductPick);
        QueryWrapper<WmsProductPick> wrapper = QueryWrapperUtil.build(wmsProductPick);
        IPage<WmsProductPick> list = wmsProductPickService.page(page, wrapper);
        return Result.success(list);
    }


    @Operation(summary = "获取生产领料单详细信息", description = "获取生产领料单详细信息")
    @GetMapping(value = "/{id}")
    public Result getInfo(@Parameter(description = "生产领料单ID") @PathVariable("id") Long id) {
        return Result.success(wmsProductPickService.getById(id));
    }
    @Operation(summary = "新增生产领料单", description = "新增生产领料单")
    @PostMapping
    public Result<?> save(@Parameter(description = "生产领料单") @RequestBody WmsProductPickVO wmsProductPickVO) {
        wmsProductPickService.saveProductPick(wmsProductPickVO);
        return Result.success();
    }

    @Operation(summary = "修改生产领料单", description = "修改生产领料单")
    @PutMapping
    public Result<?> update(@Parameter(description = "生产领料单") @RequestBody WmsProductPickVO wmsProductPickVO) {
        wmsProductPickService.updateProductPick(wmsProductPickVO);
        return Result.success();
    }

    @Operation(summary = "删除生产领料单", description = "删除生产领料单")
    @DeleteMapping
    public Result<?> remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        return Result.success(wmsProductPickService.removeByIds(Arrays.asList(ids)));
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<WmsProductPick> util = new ExcelUtil<>(WmsProductPick.class);
        util.importTemplateExcel(response, "生产领料单");
    }

    @Operation(summary = "导出生产领料单列表", description = "导出生产领料单列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsProductPickParam wmsProductPickParam) {
        WmsProductPick wmsProductPick = new WmsProductPick();
        BeanUtils.copyProperties(wmsProductPickParam, wmsProductPick);
        QueryWrapper<WmsProductPick> wrapper = QueryWrapperUtil.build(wmsProductPick);
        List<WmsProductPick> list = wmsProductPickService.list(wrapper);
        ExcelUtil<WmsProductPick> util = new ExcelUtil<>(WmsProductPick.class);
        util.exportExcel(response, list, "生产领料单数据");
    }

    @Operation(summary = "导入生产领料单列表", description = "导入生产领料单列表")
    @PostMapping("/import")
    public Result<?> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<WmsProductPick> util = new ExcelUtil<>(WmsProductPick.class);
        List<WmsProductPick> wmsProductPickList = util.importExcel(file.getInputStream());
        String message = wmsProductPickService.importData(wmsProductPickList, updateSupport);
        return Result.success(message);
    }
}